﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public class Common
    {
        public static string FormatTextArea(String str)
        {
            str = str.Replace("\n", "<br/>");
            str = str.Replace(" ", "&nbsp;");
            return str;
        }

        //年假计算
        public int GetNianJia(string empId)
        {
            int nianJia = 0;
            int eId = int.Parse(empId);
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            Employee employee = ctx.Employees.Single(r=>r.Id==eId);

            //进入公司工作时间
            DateTime startWorkDate = (DateTime)employee.TrialDate;
            //进入公司前工作天数
            int beforeWorkDays = 0;

            IQueryable<EmpWork> iemworks = ctx.EmpWorks.Where(r => r.EmpId == eId).OrderBy(r => r.StartDate);
            if (iemworks != null)
            {
                foreach (EmpWork ep in iemworks)
                {
                    DateTime start = ep.StartDate;
                    DateTime end = (DateTime)ep.EndDate;
                    beforeWorkDays += (end - start).Days;
                }
            }
            DateTime oneYear = startWorkDate.AddYears(1);//太阳神 入职1年 临界点
            DateTime tenYear = startWorkDate.AddDays(3650 - beforeWorkDays);
            DateTime twentyYear = startWorkDate.AddDays(7300 - beforeWorkDays);//总工龄20年临界点
            DateTime today = DateTime.Today;
            DateTime firstDay=new DateTime(today.Year,1,1);
            DateTime lastDay=new DateTime(today.Year,12,31);
            DateTime criticalDay=new DateTime(today.Year,oneYear.Month,oneYear.Day);//临界日
            if(oneYear<today)
            {
                int totalDays =(lastDay - firstDay).Days;//今年天数
                int beforeDays = (criticalDay - firstDay).Days;//临界点之前天数
                int backDays = (lastDay - criticalDay).Days;//临界点之后天数
                if(oneYear.Year==today.Year)
                {
                    int beforeDays2 = (criticalDay - oneYear).Days;//临界点之前天数
                    if (tenYear.Year == today.Year && tenYear > criticalDay)
                    {
                        nianJia = (beforeDays2 * 5 + backDays * 10) / totalDays;
                    }
                    else if (twentyYear.Year == today.Year && twentyYear > criticalDay)
                    {
                        nianJia = (beforeDays2 * 10 + backDays * 15) / totalDays;
                    }
                    else
                    {
                        if (twentyYear < today)
                        {
                            nianJia = backDays * 15 / totalDays;
                        }
                        else if (tenYear < today)
                        {
                            nianJia = backDays *10/ totalDays;
                        }
                        else
                        {
                            nianJia =backDays *5/ totalDays;
                        }
                    }
                }
                else if (tenYear.Year == today.Year)
                {
                    nianJia = (beforeDays * 5 + backDays * 10) / totalDays;
                }
                else if (twentyYear.Year == today.Year)
                {
                    nianJia =(beforeDays*10+ backDays*15)/totalDays;
                }
                else
                {
                    if(twentyYear<today)
                    {
                        nianJia = 15;
                    }
                    else if(tenYear<today)
                    {
                        nianJia = 10;
                    }
                    else
                    {
                        nianJia = 5;
                    }
                }
            }

            return nianJia;
        }
    }
}